const { Sequelize } = require('sequelize');
const config = {
  host: "localhost",
  database: "exam",
  username: "root",
  password: "",
  port: 3306
}
const sequelize = new Sequelize( //实例
  config.database,
  config.username,
  config.password,
  {
    host: config.host,
    dialect: "mysql",
    pool: {
      max: 5,//最大连接池连接数量
      min: 0,
      idle: 10000,//如果一个线程10秒内没被使用则释放
    },
    logging: true,//
  }
)
const UserModel = require('./model/user')(sequelize)
const questionModel = require('./model/question')(sequelize)
const paperModel = require('./model/paper')(sequelize)
const courseModel = require('./model/course')(sequelize)
const examModel = require('./model/exam')(sequelize)
const wrongModel = require('./model/wrong')(sequelize)

UserModel.hasMany(courseModel, {foreignKey: 'userId'})
courseModel.belongsTo(UserModel, {foreignKey: 'userId'})

UserModel.hasMany(examModel, {foreignKey: 'stuId'})
examModel.belongsTo(UserModel, {foreignKey: 'stuId'})


paperModel.hasMany(examModel, {foreignKey: 'paperId'})
examModel.belongsTo(paperModel, {foreignKey: 'paperId'})


questionModel.hasMany(wrongModel, {foreignKey: 'questionId'})
wrongModel.belongsTo(questionModel, {foreignKey: 'questionId'})

module.exports = {
  sequelize,
  UserModel,
  questionModel,
  paperModel,
  courseModel,
  wrongModel,
  examModel
}
